package com.example.demo.service;

import com.example.demo.entity.Memorandum;
import com.example.demo.entity.OrderBase;
import com.example.demo.repository.MemorandumRepository;
import com.example.demo.repository.StatusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MemorandumService {
    @Autowired
    MemorandumRepository memorandumRepository;
    @Autowired
    StatusRepository statusRepository;

    public Memorandum getMemorandum(Integer id){
        Memorandum memorandum = memorandumRepository.findOne(id);
        return memorandum;
    }

    public Memorandum saveMemorandum(Map memorandum, HttpServletRequest request){
        Memorandum newMemorandum = checkMemorandum(memorandum,request);
        Memorandum save = memorandumRepository.save(newMemorandum);
        return save;
    }
    public Memorandum checkMemorandum(Map memorandum, HttpServletRequest request){
        Memorandum memo = new Memorandum();
        if( !StringUtils.isEmpty(memorandum.get("id").toString()) ){
            memo = memorandumRepository.findOne(Integer.valueOf(memorandum.get("id").toString()));
        }
        else{
           memo.setMemorandumStatus(statusRepository.findOne(9));
        }
        if( !StringUtils.isEmpty(memorandum.get("memorandumName").toString()) ){
            memo.setMemorandumName(memorandum.get("memorandumName").toString() );
        }
        if( !StringUtils.isEmpty(memorandum.get("memorandumContent").toString()) ){
            memo.setMemorandumContent(memorandum.get("memorandumContent").toString() );
        }
        if( memorandum.get("memorandumStart") != null ){
            Long memoStart = Long.valueOf(memorandum.get("memorandumStart").toString());
            memo.setMemorandumStart( new Date(memoStart) );
        }
        memo.setUpdateTime(new Date());
        HttpSession session = request.getSession();
        String userId = session.getAttribute("user").toString();
        memo.setUpdater(userId);
        return memo;
    }

    public Page<Memorandum> findMemorandumCriteria(Integer page, Integer size, final Map memorandumQuery,String userId) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");
        Page<Memorandum> customerPage = memorandumRepository.findAll(new Specification<Memorandum>() {
            public Predicate toPredicate(Root<Memorandum> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> lstPredicates = new ArrayList<Predicate>();
                if( !StringUtils.isEmpty(memorandumQuery.get("memorandumName")) ){
                    lstPredicates.add(criteriaBuilder.like(root.get("memorandumName").as(String.class), "%"+memorandumQuery.get("memorandumName")+"%"));
                }
                lstPredicates.add(criteriaBuilder.equal(root.get("updater").as(String.class), userId));
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        }, pageable);
        return customerPage;
    }

    public Boolean memorandumDelete(Integer id){
        memorandumRepository.delete(id);
        return true;
    }

    public void memorandumDone(Integer id){
        Memorandum memorandum = memorandumRepository.findOne(id);
        memorandum.setMemorandumStatus(statusRepository.findOne(10));
        memorandumRepository.save(memorandum);
    }
    public Integer unDoneCount(Integer id){
        int count   = memorandumRepository.unDoneCount(id);
        return count;
    }
}
